package rd.hardcore.ui.config.content.boards 
{
	import aze.motion.eaze;
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.filesystem.File;
	import flash.net.URLRequest;
	import rd.hardcore.engine.datas.StreamBoardManager;
	import rd.hardcore.engine.tools.Tools;
	
	/**
	 * ...
	 * @author Romain Prache
	 */
	public class BoardItem extends BoardItem_FC
	{
		private var l 		: Loader;
		private var s		: Sprite;
		private var b		: Bitmap;
		private var id		: String;
		private var label	: String;
		
		private var active	: Boolean;
		
		
		public function BoardItem() 
		{			
			l = new Loader();
			l.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
			l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, fail);
			
			mouseChildren = false;
			buttonMode = true;
		}
	
		
		public function build( _xml : XML ) : void
		{
			label = _xml.@label;
			
			label_tf.htmlText = label.toUpperCase();
			id = _xml.@id;
			active = _xml.@state == "true" ? true : false;
			l.load( new URLRequest( Tools.macOsUrlProofing(File.applicationStorageDirectory.resolvePath( _xml.toString() ).nativePath)) ) ;
			
			if ( active )
				select();
			else
				unselect();
		}

		
		
		/* LOADING */
		private function handleComplete(e:Event):void 
		{
			cleanListeners();
			
			b = l.content as Bitmap;
			b.y = 25;
			addChild(b);
			
			addEventListener( MouseEvent.CLICK, 	handleClick);
			addEventListener( MouseEvent.ROLL_OVER, handleOver);
			addEventListener( MouseEvent.ROLL_OUT, 	handleOut);
		}
		
		private function fail(e:IOErrorEvent):void 
		{
			cleanListeners();
		}
		
		private function cleanListeners() : void
		{
			l.contentLoaderInfo.removeEventListener(Event.COMPLETE, handleComplete);
			l.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, fail);
		}
		
		private function select() : void
		{
			removeEventListener( MouseEvent.ROLL_OVER, handleOver);
			removeEventListener( MouseEvent.ROLL_OUT, 	handleOut);
			
			tf.alpha = 0;
			tf.htmlText = "BOARD ACTIVATED";
			eaze(tf).to(.4, { alpha:1 } );
		}
		
		private function unselect() : void
		{
			addEventListener( MouseEvent.ROLL_OVER, handleOver);
			addEventListener( MouseEvent.ROLL_OUT, 	handleOut);
			
			tf.alpha = 0;
			tf.htmlText = "ACTIVATE THIS BOARD";
			eaze(tf).to(.4, { alpha:1 } );
		}
		
		/* MOUSE EVENTZ */
		private function handleClick(e:MouseEvent):void 
		{
			active = StreamBoardManager.instance.setBoardsState( id );
			
			if (active)
				select();
			else
				unselect();
		}
		
		
		
		private function handleOver(e:MouseEvent):void 
		{
			eaze(this).to(.5, { alpha:.8 } );
		}
		
		private function handleOut(e:MouseEvent):void 
		{
			eaze(this).to(.5, { alpha:1 } );
		}
		
		
		
		
		
	}

}